package com.huatai.bi.controller;

import com.huatai.bi.service.ImportExcelService;
import com.huatai.bi.utils.StringBuildUtil;
import com.huatai.bi.vo.excelVo.ExcelDataVO;
import com.huatai.bi.vo.excelVo.SubClassVO;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.huatai.bi.controller
 * @Author: hecaiy
 * @CreateTime: 2024-06-13  10:11
 * @Description: TODO  excel上传相关接口
 * @Version: 1.0
 */
@RestController
@Api(value = "excel上传",tags = "excel上传")
@RequestMapping("/excel")
public class ImportExcelController {

    @Autowired
    private ImportExcelService importExcelService;

    @PostMapping("/updateFileDataSet")
    @ApiOperation(value = "上传文件，创建数据集")
    public R<Boolean> updateFileDataSet(@RequestParam(value = "file") MultipartFile file,
                                            @RequestParam(value = "sheet") String sheetName,
                                            @RequestParam(value = "dtId") Long dtId) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".csv") && !originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")){
            throw new ServiceException("文件格式错误,只支持Excel/CSV格式");
        }
        StringBuilder stringBuilder = StringBuildUtil.originalFilename(originalFilename.replace(".csv", "").replace(".xls", "").replace(".xlsx", ""));
        if(stringBuilder.length()>0){
            throw new ServiceException("文件名称不允许有特殊字符，特殊字符为"+stringBuilder.toString());
        }
        return R.data(importExcelService.updateFileDataSet(file,sheetName,dtId));
    }

    @PostMapping("/createFileDataSet")
    @ApiOperation(value = "上传文件，创建数据集")
    public R<Boolean> createFileDataSet(@RequestParam(value = "file") MultipartFile file,
                                        @RequestParam(value = "sheet") List<String> sheets,
                                        @RequestParam(value = "dtId") Long dtId) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".csv") && !originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")){
            throw new ServiceException("文件格式错误,只支持Excel/CSV格式");
        }
        StringBuilder stringBuilder = StringBuildUtil.originalFilename(originalFilename.replace(".csv", "").replace(".xls", "").replace(".xlsx", ""));
        if(stringBuilder.length()>0){
            throw new ServiceException("文件名称不允许有特殊字符，特殊字符为"+stringBuilder.toString());
        }
        return R.data(importExcelService.createFileDataSet(file,sheets,dtId));
    }

    @PostMapping("/getExcelImportSheetTree")
    @ApiOperation(value = "上传文件，获取左侧树结构")
    public R<ExcelDataVO> getExcelImportSheetTree(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".csv") && !originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")){
            throw new ServiceException("文件格式错误,只支持Excel/CSV格式");
        }
        StringBuilder stringBuilder = StringBuildUtil.originalFilename(originalFilename.substring(0, originalFilename.lastIndexOf(".")));
        if(stringBuilder.length()>0){
            throw new ServiceException("文件名称不允许有特殊字符，特殊字符为"+stringBuilder.toString());
        }
        return R.data(importExcelService.getExcelImportSheetTree(file));
    }

    @PostMapping("/getExcelImportSheetData")
    @ApiOperation(value = "上传文件，根据sheet获取sheet数据")
    public R<SubClassVO> getExcelImportSheetData(@RequestParam(value = "file") MultipartFile file,
                                                 @RequestParam(value = "sheetName") String sheetName) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".csv") && !originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")){
            throw new ServiceException("文件格式错误,只支持Excel/CSV格式");
        }
        StringBuilder stringBuilder = StringBuildUtil.originalFilename(originalFilename.substring(0, originalFilename.lastIndexOf(".")));
        if(stringBuilder.length()>0){
            throw new ServiceException("文件名称不允许有特殊字符，特殊字符为"+stringBuilder.toString());
        }
        return R.data(importExcelService.getExcelImportSheetData(file,sheetName));
    }

    /**
     * 未使用到
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传接口，返回树结构+数据")
    public R<ExcelDataVO> upload(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".csv") && !originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")){
            throw new ServiceException("文件格式错误,只支持Excel/CSV格式");
        }
        StringBuilder stringBuilder = StringBuildUtil.originalFilename(originalFilename.substring(0, originalFilename.lastIndexOf(".")));
        if(stringBuilder.length()>0){
            throw new ServiceException("文件名称不允许有特殊字符，特殊字符为"+stringBuilder.toString());
        }
        return R.data(importExcelService.importExcel(file,""));
    }
}
